package spark.example

import scala.Iterator
import org.apache.spark.SparkConf
import org.apache.spark.SparkContext

object Test_mapPartitions {
	def main(args: Array[String]) {

	    val conf = new SparkConf().setAppName("Test mapPartitions")
        val spark = new SparkContext(conf)
        val input = spark.parallelize(List(1,2,3,4,5,6),2)
        val result = input.mapPartitions(partition => Iterator(sumOfEveryPartition(partition)))
        result.collect().foreach(println(_))	
	}
    def sumOfEveryPartition(input: Iterator[Int]): Int = {
        var total = 0
        input.foreach { elem =>
            total += elem
        }
        return total
    }
}
